''' Mission 10 - Reaction Tester Extra Spicy solution - 4A Option Make the reaction tester into a game by comparing time to the average. Play the game 20 times. If the score is at least 15, player wins. This solution uses the pixels for the indicator, but students could use color or images as well. This solution also includes the functions for intro and countdown. ''' from codex import * import random import time def wait_button(): while True: if buttons.was_pressed(BTN_A): break # -- add intro function def intro(): display.print("Reaction Tester") display.print("When the pixels") display.print("turn GREEN") display.print("press BTN A") display.print() display.print("Press A to start") wait_button() display.clear() def countdown(): # clear screen and countdown display.clear() pixels.set([BLACK, BLACK, BLACK, BLACK]) display.print("3", scale=6) time.sleep(1) display.print("2", scale=6) time.sleep(1) display.print("1", scale=6) time.sleep(1) display.clear() # -- call intro AVG = 200 # average of reaction times (can vary) score = 0 loops = 0 END_GAME = 10 WIN_SCORE = 7 intro() # play the game 10 times while loops < END_GAME: display.print("Press Button A") wait_button() countdown() # get random delay time ms = random.randrange(1000, 5000) delay_time = ms / 1000 time.sleep(delay_time) # turn pixels GREEN buttons.was_pressed(BTN_A) pixels.set([GREEN, GREEN, GREEN, GREEN]) # get start and end time start_time = time.ticks_ms() wait_button() end_time = time.ticks_ms() reaction_time = time.ticks_diff(end_time, start_time) if reaction_time <= AVG: score = score + 1 display.print("Reaction Time: ") display.print(reaction_time) display.print("Score: " + str(score)) display.print() # increment the control variable loops = loops + 1 # results of game display.clear() display.print("Score: " + str(score)) if score >= WIN_SCORE: display.print("You WIN", scale=3) else: display.print("You lose", scale=3)